package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.YhMemberLevelRule;
import com.ruoyi.system.service.IYhMemberLevelRuleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 会员等级规则Controller
 * 
 * @author ruoyi
 * @date 2023-06-06
 */
@Api(tags = "会员等级规则管理")
@RestController
@RequestMapping("/yh/level/rule")
public class YhMemberLevelRuleController extends BaseController
{
    @Autowired
    private IYhMemberLevelRuleService yhMemberLevelRuleService;

    /**
     * 查询会员等级规则列表
     */
    @ApiOperation("获取会员等级规则列表")
    @GetMapping("/list")
    public TableDataInfo list(YhMemberLevelRule yhMemberLevelRule)
    {
        startPage();
        List<YhMemberLevelRule> list = yhMemberLevelRuleService.list(yhMemberLevelRule.queryWrapper());
        return getDataTable(list);
    }

    /**
     * 导出会员等级规则列表
     */
    @ApiOperation("导出会员等级规则列表")
    @PreAuthorize("@ss.hasPermi('yh:rule:export')")
    @Log(title = "会员等级规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhMemberLevelRule yhMemberLevelRule)
    {
        List<YhMemberLevelRule> list = yhMemberLevelRuleService.list(yhMemberLevelRule.queryWrapper());
        ExcelUtil<YhMemberLevelRule> util = new ExcelUtil<YhMemberLevelRule>(YhMemberLevelRule.class);
        util.exportExcel(response, list, "会员等级规则数据");
    }

    /**
     * 获取会员等级规则详细信息
     */
    @ApiOperation("获取会员等级规则详细")
    @ApiImplicitParam(name = "id", value = "会员等级规则ID", required = true, dataType = "String", paramType = "path")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(yhMemberLevelRuleService.getById(id));
    }

    /**
     * 新增会员等级规则
     */
    @ApiOperation("新增会员等级规则")
    @PreAuthorize("@ss.hasPermi('yh:rule:add')")
    @Log(title = "会员等级规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YhMemberLevelRule yhMemberLevelRule)
    {
        YhMemberLevelRule one = yhMemberLevelRuleService.getOne(new LambdaQueryWrapper<YhMemberLevelRule>()
                .eq(YhMemberLevelRule::getMemberLevel, yhMemberLevelRule.getMemberLevel()));
        if(StringUtils.isNotNull(one)){
            throw new ServiceException("会员等级已存在！");
        }
        return toAjax(yhMemberLevelRuleService.save(yhMemberLevelRule));
    }

    /**
     * 修改会员等级规则
     */
    @ApiOperation("更新会员等级规则")
    @PreAuthorize("@ss.hasPermi('yh:rule:edit')")
    @Log(title = "会员等级规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhMemberLevelRule yhMemberLevelRule)
    {
        return toAjax(yhMemberLevelRuleService.updateById(yhMemberLevelRule));
    }

    /**
     * 删除会员等级规则
     */
    @ApiOperation("删除用户信息")
    @ApiImplicitParam(name = "ids", value = "会员等级规则ID集合", required = true, dataType = "String", paramType = "path")
    @PreAuthorize("@ss.hasPermi('yh:rule:remove')")
    @Log(title = "会员等级规则", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids)
    {
        return toAjax(yhMemberLevelRuleService.removeByIds(ids));
    }
}
